package com.renchunlin.taobaounion.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.model.domain.HomePagerContent;
import com.renchunlin.taobaounion.model.domain.ILinaerItemInfo;
import com.renchunlin.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import static android.graphics.Paint.STRIKE_THRU_TEXT_FLAG;

public class LinearItemContentAdapter extends BaseQuickAdapter<ILinaerItemInfo, BaseViewHolder> {

    private Context mContext;
    private List<ILinaerItemInfo> datas = new ArrayList<>();

    public LinearItemContentAdapter(int layoutResId, @Nullable List<ILinaerItemInfo> data, Context mContext) {
        super(layoutResId, data);
        this.mContext = mContext;
        this.datas = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, ILinaerItemInfo item) {
        helper.setText(R.id.goods_title, item.getTitle());

        /*ViewGroup.LayoutParams layoutParams = helper.getView(R.id.goods_cover).getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        int coverSize = (width > height ? width : height) / 2;*/
        String cover = item.getCover();
        if (!TextUtils.isEmpty(cover)) {
            Glide.with(mContext).load(UrlUtils.getCoverPath(cover)).into((ImageView) helper.getView(R.id.goods_cover));
        } else {
            helper.setBackgroundRes(R.id.goods_cover, R.mipmap.ic_launcher);
        }
        String zk_final_price = item.getFinalPrise();
        int couponAmount = item.getCouponAmount();
        helper.setText(R.id.goods_off_prise, String.format(mContext.getString(R.string.text_goods_off_prise), couponAmount));
        float resultPrise = Float.parseFloat(zk_final_price) - couponAmount;
        helper.setText(R.id.goods_after_off_prise, String.format("%.2f", resultPrise));
        //设置文字中划线
        ((TextView) helper.getView(R.id.goods_original_prise)).setPaintFlags(STRIKE_THRU_TEXT_FLAG);
        helper.setText(R.id.goods_original_prise, String.format(mContext.getString(R.string.text_goods_original_prise), zk_final_price));
        helper.setText(R.id.goods_sell_count, String.format(mContext.getString(R.string.text_goods_sell_count), item.getVolume()));
    }

    public void addListData(List<? extends ILinaerItemInfo> data) {
        int size = datas.size();
        datas.addAll(size, data);
        notifyItemRangeChanged(size, data.size());
    }
}
